const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({
  data: {
    banner:[],
    navHeight:'',
    page:1,
    pageSize:10,
    isEnd:false,
    goodslistdata:[],
    category:[],
    type:1,
    categoryId:'',
    mallindex:0,
    notice:[],
    isloading:true
  },
  onLoad(options) {
    this.setData({
      navHeight: app.globalData.navHeight
    })
    this.bannerList()
    this.categorylist()
    this.noticelist()
    if(options.parentid){
      app.globalData.parentid=options.parentid
    }
  },
  goLink(e){
   let url=e.currentTarget.dataset.url
   if(url){
     wx.navigateTo({
       url
     })
   }
  },
  noticelist(){
    Controller.noticelist().then(res=>{
      if(res.data.baseData){
        this.setData({
          notice:res.data.baseData
        })
      }
    })
  },
  bannerList(){
    Controller.bannerList({source:2}).then(res=>{
      if(res.data.baseData){
        this.setData({
          banner:res.data.baseData
        })
      }
    })
  },
  changemall(e){
    var index=e.currentTarget.dataset.index;
    var title=e.currentTarget.dataset.title || '';
    var id=e.currentTarget.dataset.id || '';
    this.setData({
      page:1,
      pageSize:10,
      isEnd:false,
      goodslistdata:[],
      mallindex:index,
      categoryId:id
    })
    this.goodslist()
  },
  categorylist(){
    Controller.categorylist({source:5,parentId:0}).then(res=>{
      if(res.data.baseData.length>0){
        var lists=[{
          id:'',
          title:'全部'
        }]
        lists=lists.concat(res.data.baseData)
        console.log(res.data.baseData)
        this.setData({
          categorylistdata:lists,
          categoryId:lists[0].id
        })
        this.goodslist()
      }
    })
  },
  golink(e){
    if(!wx.getStorageSync('logintoken')){
      app.ToLogin()
      return
    }
    wx.navigateTo({
      url:e.currentTarget.dataset.url
    });
  },
  golink2(e){
    console.log(e)
    // var type=e.currentTarget.dataset.type;
    var item =e.currentTarget.dataset.item;
      console.log(item)

      if(item.type == 0){return;}


      if(item.type == 1 || item.type == 2||item.type == 8){
          wx.navigateTo({
            url:"/pages/news/detail?id="+item.rid
          });
      }else if(item.type == 3 ||item.type == 7){
        wx.navigateTo({
          url:"/pages/videoDetails/videoDetails?id="+item.rid
        });
      }else if(item.type == 4){
        wx.navigateTo({
          url:"/pages/mall/detail?id="+item.rid
        });
      }else if(item.type == 5){
        wx.navigateTo({
          url:"/pages/recipesDetails/recipesDetails?id="+item.rid
        });
      }else if(item.type == 6){
        // console.log(item.content)
        wx.setStorageSync('bannerContent',item.content)
        wx.navigateTo({
          url:"/pages/doc/doc?banner=true"
        });
        
      }
    
  },
  golink3(e){
    if(!wx.getStorageSync('logintoken')){
      app.ToLogin()
      return
    }
    var id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url:"/pages/mall/detail?id="+id
    });
  },
  gocart(e){
    var data={
      goodsId:e.currentTarget.dataset.id,
      specs:e.currentTarget.dataset.specs,
      amount:1,
      type:1
    }
    Controller.addOrUpdate(data).then(res=>{
      wx.showToast({
        title:"加购成功"
      })
    })
   
  },
  goodslist(){
    var data={
      keyword:"",
      page:this.data.page,
      pageSize:this.data.pageSize,
      categoryId:this.data.categoryId,
      isRecommend:""
    }
    
    Controller.goodslist(data).then(res=>{
      if(res.data.baseData && res.data.baseData.list.length>0){
        this.setData({
          goodslistdata:this.data.goodslistdata.concat(res.data.baseData.list),
        })
      }
      if(res.data.baseData.lastPage){
        this.setData({
          isEnd:res.data.baseData.lastPage
        })
      }
      this.setData({
        isloading:false
      })
    })
  },
  onReachBottom(){
    
    if(this.data.isEnd){
      return
    }else{
      this.setData({
        page:this.data.page+1
      })
      this.goodslist()
    }
    
  },
  onPageScroll: function (e) { // 获取滚动条当前位置
    
  },
  onShareAppMessage: function (e) {
    let title = '';
    let imageUrl = '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
  onShareTimeline: function(res){
    let title = '';
    let imageUrl = '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
})